/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
 * TrainingBook
 * Copyright (C) Arnaud Duval 2010 <arnaud.duval@gmail.com>
 * 
 * TrainingBook is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * TrainingBook is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "duration-entry.hpp"
#include <sstream>

///@brief Default constructor
DurationEntry::DurationEntry()
{
	set_text("0:00:00");
}

///@brief Destructor
DurationEntry::~DurationEntry()
{
	
}

///@brief Set the duration value
///@param d Duration value to set
void DurationEntry::SetValue(Duration d)
{
	value = d;
	std::string text = "";
	std::ostringstream streamHour;
	streamHour << value.GetHour();
	text += streamHour.str();
	text += ":";
	if(value.GetMinute() < 10)
		text += "0";
	std::ostringstream streamMinute;
	streamMinute << value.GetMinute();
	text += streamMinute.str();
	text += ":";
	if(value.GetSecond() < 10)
		text += "0";
	std::ostringstream streamSecond;
	streamSecond << value.GetSecond();
	text += streamSecond.str();
	set_text(text);
}

///@brief Return the value
Duration DurationEntry::GetValue()
{
	return value;
}

///@brief Update the value when the content of the entry is changed
void DurationEntry::on_changed()
{
	std::string text = get_text();
	value.SetWithFormat(text, "h:nn:ss");
}